package com.cykj.eurekaclient.controller;

import com.alibaba.fastjson.JSONObject;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePrecreateRequest;
import com.alipay.api.request.AlipayTradeQueryRequest;
import com.alipay.api.response.AlipayTradePrecreateResponse;
import com.alipay.api.response.AlipayTradeQueryResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Map;

@Controller
public class PayController {
    public int a = 0;

    public String getOrderId() {
        return null;
    }

    //小程序发起支付请求
    @RequestMapping("Pay")
    @ResponseBody
    public AlipayTradePrecreateResponse Pay(@RequestBody Map<String, String> data) {
        System.out.println("进入生产端的pay");

        System.out.println("routeName:" + data.get("routeName"));

        AlipayClient alipayClient = new DefaultAlipayClient("https://openapi.alipaydev.com/gateway.do",
                "2021000118625726",
                "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCGEiTnrYJ6wOG3vUZcOzeC8s4Y3BO6hq74gwUa2uaaTWIYqr94bEcpMqfsj/HPP/qXg1+liaVNLUNWrZ+PJUT3WDBtrJTAirhbgkHWqcVftS7E6C7TIsymKDFMT9APiBHrinHS3rkj55ALrEbPMZC7H9R2o0Qw7YwZ1Kr0xEe3xzszUli9Y/FxpDjQ1wOq7Fm+o9xqBpVdKE1Vdx+DjLzsE0Tr/qhG9Jg5V5aawnZSwFmVVBsX3B3xsHwo7wMMOnf0YA0JXW9EUpUlxfsvsZIL30F1zwvj3cJp4LAHxyRisy3icQLSKGAwhWbR9SZY4YXT4TQraspr/Ul1GltckGPHAgMBAAECggEAFMhVPjCQTFT/6EqH0J5BUaQvvHzI9h4Qm2c78FjEbGDYdvho69LlCQbYLvx1QGorfzcXKYL3w8njaVlKpZbiZPEfuLCK8nnOcCj16b+xX8+nK1DOiV4YmR0b3zd0CiROmtO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ikVSDGUNuIJ9E=",
                "json", "GBK",
                "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAq8IorGmkb2+h23qG5JXwNr855dOL71h8mGq90G+uCAT/ZDAD7D+zwdFkZrjTpZNpYVnpKhBeHKV8j2KzH+M8Sb7J894PZBejWSsqaRrfkC2l1lQG02/fKL7WfQftsTfoHCKjpgtWfmenZeHBMZc0NnQjmT4g0EsD6sZK3RYoQ5YXFadJd8i6OMwitfYwLC1HRO90yIALoMa6qtXxczi0NGJDaWBafEYEOEI3QG1v5qgijFPK34w78imDiIxdMKehvcalRBXKBJtqK87UUZpSJLlht8buxva715mKa0FP4WF0D/SN0CljtusfyAeluU54d3f1jfWqG9tE6m2yqRvs4QIDAQAB",
                "RSA2");
        AlipayTradePrecreateRequest request = new AlipayTradePrecreateRequest();
        request.setNotifyUrl("");
        JSONObject bizContent = new JSONObject();
//        data.get("routeName")
        bizContent.put("out_trade_no", a);
        a++;
        bizContent.put("total_amount", 1);
        bizContent.put("subject", "公交支付:" + data.get("routeName"));
        bizContent.put("qr_code_timeout_express", "10m");

        request.setBizContent(bizContent.toString());
        System.out.println("+++" + request);
        AlipayTradePrecreateResponse response = null;
        try {
            response = alipayClient.execute(request);

        } catch (AlipayApiException e) {
            e.printStackTrace();
        }
        if (response.isSuccess()) {
            System.out.println(response + "调用成功");
        } else {
            System.out.println(response + "调用失败");
        }

        return response;
    }


    @RequestMapping("vmPayTest")
    @ResponseBody
    public AlipayTradeQueryResponse vmPayTest(@RequestBody String data) {
        System.out.println(data);
        JSONObject jsonObject = JSONObject.parseObject(data); //转为JSON 格式
        String outTradeNo = jsonObject.getString("outTradeNo");
        System.out.println(outTradeNo);
        AlipayClient alipayClient = new DefaultAlipayClient("https://openapi.alipaydev.com/gateway.do",
                "2021000118625726",
                "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCGEiTnrYJ6wOG3vUZcOzeC8s4Y3BO6hq74gwUa2u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4PpW5kJDU5r7Wok5lNfWtU8MVYlYijh4CeAPh0yoSbrj+HoU6MHazXJzUxh3RSAnMnmAxsO0pfwakhAIAPdldbUTACI0GV72X3tTDccHo/SHu/3ic+kLXxAoGAV0AfQcZhyZqSzwM2ySfSfTM9rHFzc87SYWpjwF3KSARQsNGmoSBFO60EGmI+KQ+mRubMMjlUuWJC770JoFcRjcwBtqjuQ09n04Hwq0RYWK+tL3ggce6k5PwH6YbtdwpC68H6VoWimbXo/pNO8BONSAgF/QvaTQYdzESdkbHEYKECgYEAhoQ03fqWKwHnszhiUbxDuiqQ9znzGw0r/E90+u5zKBVd07nTsYsZRh/1toWBgV+dsGsb48redJqlrVuMX2tfPzZ0YTdDTuGsVrkEpJke2Vc3BGT/xy7CLd8p+0uDd1ccJnNAEE7zzPnMytYSn6qbn3x9l9STF9ikVSDGUNuIJ9E=",
                "json", "GBK",
                "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAq8IorGmkb2+h23qG5JXwNr855dOL71h8mGq90G+uCAT/ZDAD7D+zwdFkZrjTpZNpYVnpKhBeHKV8j2KzH+M8Sb7J894PZBejWSsqaRrfkC2l1lQG02/fKL7WfQftsTfoHCKjpgtWfmenZeHBMZc0NnQjmT4g0EsD6sZK3RYoQ5YXFadJd8i6OMwitfYwLC1HRO90yIALoMa6qtXxczi0NGJDaWBafEYEOEI3QG1v5qgijFPK34w78imDiIxdMKehvcalRBXKBJtqK87UUZpSJLlht8buxva715mKa0FP4WF0D/SN0CljtusfyAeluU54d3f1jfWqG9tE6m2yqRvs4QIDAQAB",
                "RSA2");
        AlipayTradeQueryRequest request = new AlipayTradeQueryRequest();
        JSONObject bizContent = new JSONObject();
        bizContent.put("out_trade_no", outTradeNo);
        request.setBizContent(bizContent.toString());
        AlipayTradeQueryResponse response = null;
        try {
            response = alipayClient.execute(request);
        } catch (AlipayApiException e) {
            e.printStackTrace();
        }
        if (response.isSuccess()) {
            System.out.println("调用成功");
        } else {
            System.out.println("调用失败");
        }
        return response;
    }
}
